package moddb;

import java.sql.*;
import java.util.HashMap;

public class Record {

  private Driver jdbcDriver = null;
  private Connection conn = null;
  
  private Record(String driverName, String connUrl) {
    
    try {
      // Load database driver
      jdbcDriver = (Driver) Class.forName(driverName).newInstance();
    } catch (Exception e) {
      throw new RuntimeException("Could not find database driver");
    }

    try {
      // Get connection to the database
      conn = DriverManager.getConnection(connUrl);
    } catch (Exception e) {
      throw new RuntimeException("Could not get a connection to the database");
    }

    if (conn == null) {
      throw new RuntimeException("Could not connect to " + connUrl);
    }
  }
  
  /*public String getDriverName() {
    return driverName;
  }
  
  public String getConnUrl() {
    return connUrl;
  }*/
  
  public Connection getConnection() {
    return conn;
  }
  
  private static Record currentRecord = null;
  private static HashMap<String, Record> connUrlMap = new HashMap<String, Record>();
  
  public static Record getRecord(String connUrl) {
    if (connUrlMap.containsKey(connUrl)) {
      return connUrlMap.get(connUrl);
    } else {
      Record r = new Record("org.sqlite.JDBC", connUrl);
      connUrlMap.put(connUrl, r);
      return r;
    }
  }
  
}
